﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WickedSick.ClientComponents.Music;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Threading;

namespace SDB
{
    /// <summary>
    /// Interaction logic for SongDisplay.xaml
    /// </summary>
    public partial class SongDisplay : Window
    {
        private MediaBI media;
        private ObservableCollection<string> items = new ObservableCollection<string>();
        private CollectionView view;

        public SongDisplay()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(SongDisplay_Loaded);
            for (int i = 0; i < 300; i++)
            {
                items.Add(i.ToString());
            }
        }

        void SongDisplay_Loaded(object sender, RoutedEventArgs e)
        {
            media = new MediaBI("media.sdb");
            media.CommunicationComplete += new CommunicationCompleteDelegate(media_CommunicationComplete);
            if (media.Bank.Artists.Count == 0)
            {
                media.InitializeMedia(1);
            }
        }

        void media_CommunicationComplete(object sender, int CommunicationType)
        {
            if (CommunicationType == 1)
            {
                media.SendMediaToFile();
            }
        }

        private void Songs_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            //SongScroll.ScrollToVerticalOffset(SongScroll.VerticalOffset - ((double)e.Delta/2));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Songs.ItemsSource = items;
            SongTree.ItemsSource = media.Bank.Artists.Values;
            
            //view = (System.Windows.Data.CollectionView)CollectionViewSource.GetDefaultView(SongTree.ItemsSource);
            //view.Filter = delegate(object item)
            //{
            //    Artist artist = (Artist)item;
            //    foreach (Album album in artist.Albums.Values)
            //    {
            //        foreach (Song song in album.Songs.Values)
            //        {
            //            return (song.SongLocation != SongLocations.Library);
            //        }
            //    }
            //    return false;
            //};
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            view.Filter = delegate(object item)
            {
                Artist artist = (Artist)item;
                return (artist.Name.ToLower().Contains(ArtistFilter.Text.ToLower()));
            };

            //Thread th = new Thread(new ThreadStart(Filter));
            //th.Start();
        }

        private object AddFilter(object arg)
        {
            view.Filter = delegate(object item)
            {
                int my = int.Parse((string)item);
                return (my < 100);
            };
            return null;
        }

        private void Filter()
        {
            Application app = System.Windows.Application.Current;
            if (app != null)
            {
                app.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(AddFilter), new object());
            }
        }
    }
}
